package cn.com.test.bdocr;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
//
import com.baidu.ocr.sdk.OCR;
import com.baidu.ocr.sdk.exception.OCRError;
import com.baidu.ocr.sdk.model.ResponseResult;
import com.exocr.OCRCallBack;
import com.exocr.OCRCameraActivity;
import com.exocr.OCRTypes;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;

public class DemoActivity extends AppCompatActivity {
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                menu();
            }
        });
        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OCRCameraActivity.doOCR(DemoActivity.this,ocrType,filePath,null,null,null, new OCRCallBack() {
                    @Override
                    public void result(Object data) {
                        String msg=null;
                        if (data instanceof ResponseResult) {
                            ResponseResult rr = (ResponseResult) data;
                            msg=rr.getJsonRes();
                        } else if (data instanceof OCRError) {
                            OCRError rr = (OCRError) data;
                            msg=rr.getMessage();
                        } else if (data instanceof Map) {
                            JSONObject jsonObj=new JSONObject((Map)data);
                            msg=jsonObj.toString();
                        }
                        Toast.makeText(DemoActivity.this,msg,Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
    OCRTypes ocrType=null;
    String filePath =null;
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1000) {
            if (data!=null) {
                Serializable se = data.getSerializableExtra("result");
                Serializable se2=data.getSerializableExtra(OCRCameraActivity.OcrTypeKey);
                if (se2!=null&&se2 instanceof OCRTypes){
                    ocrType=(OCRTypes)se2;
                }
                filePath = data.getStringExtra("filePath");
                if (filePath!=null){
                    Bitmap bt=BitmapFactory.decodeFile(filePath);
                    if (bt!=null){
                        ImageView imageView=findViewById(R.id.imageView);
                        imageView.setImageBitmap(bt);
                    }else{
                        filePath=null;
                    }
                    findViewById(R.id.button2).setVisibility(bt!=null?View.VISIBLE:View.GONE);
                }

                if (resultCode == Activity.RESULT_CANCELED) {
                    if (se instanceof Exception){
                        Toast.makeText(DemoActivity.this, "" + ((Exception)se).getMessage(), Toast.LENGTH_LONG).show();
                    }
                } else {
                    if (se instanceof String){
                        Toast.makeText(DemoActivity.this, (String)se, Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
    }
    public void menu(){
        ArrayList<String> itemArray=testData();
        final String[] items=itemArray.toArray(new String[]{});
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setSingleChoiceItems(items, -1,  new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                try {
                    if (items[which].startsWith("  ")){
                        Intent it=new Intent(DemoActivity.this,OCRCameraActivity.class);
                        OCRTypes ocrType=null;
                        if (which>=xfocrStart){
                            ocrType=getXfTypes(items[which]);
                            ocrType.setAppid("595359f4");
                            ocrType.setApiKey("ebe7a44e6912916d59ad2cd9d267d5df");
                            ocrType.setApiKey_V2("9a2682375768e0eff2ec95c8ce1ed881");
                            ocrType.setApiSecretKey("2c582e8f50aacf3d78ba5fa329409c75");
                        }else{
                            ocrType=getTypes(items[which]);
                            ocrType.setApiKey("xm8yFA69ezn43tzvAPhaFXrd");
                            ocrType.setApiSecretKey("GdG0yMXEv0ct7sYAAtsEwaA4FCvb2X1j");
                        }
                        if (ocrType!=null){
                            it.putExtra(OCRCameraActivity.OcrTypeKey,ocrType);
                            DemoActivity.this.startActivityForResult(it,1000);
                            dialog.dismiss();
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
        builder.show();
    }

    /////////////
    //忽略
    /////////////
    int xfocrStart=0;
    public ArrayList<String> testData(){
        String[] commons=new String[]{"通用文字识别","简单","简单(含位置)","高精度(含生僻字)","高精度(含位置)"};
        String[] card=new String[]{"卡证文字识别","身份证(正面)","身份证(反面)","银行卡","营业执照","名片","护照","港澳通行证","台湾通行证识别","户口本识别","出生医学证明识别","多卡证类别检测"};
        String[] bill=new String[]{"票据文字识别","增值税发票","定额发票","通用机打发票","火车票","出租车票","行程单","汽车票","通行费发票","通用票据","保单","彩票"};
        String[] car=new String[]{"汽车场景文字识别","行驶证","驾驶证","车牌","VIN码","机动车销售发票","车辆合格证"};
        String[] edu=new String[]{"教育场景文字识别","手写文字","公式"};
        String[] extra=new String[]{"其他文字识别","网络图片文字","网络图片文字(含位置版)","文档版面","仪器仪表盘读数","表格文字(异步接口)","表格文字(同步接口)","数字","二维码","印章","门脸文字"};
        String[] xfocr=new String[]{"讯飞ocr识别","手写文字识别","印刷文字识别","印刷文字识别（多语种）","名片识别","身份证识别","银行卡识别","营业执照识别","增值税发票识别","拍照速算识别","公式识别","指尖文字识别"};

        ArrayList<String> itemArray=new ArrayList<>();
        itemArray.add("百度ocr识别");
        for (int i=0;i<commons.length;i++){
            String n=commons[i];
            if (i>0){
                n="    "+n;
            }
            itemArray.add(n);
        }
        for (int i=0;i<card.length;i++){
            String n=card[i];
            if (i>0){
                n="    "+n;
            }
            itemArray.add(n);
        }
        for (int i=0;i<bill.length;i++){
            String n=bill[i];
            if (i>0){
                n="    "+n;
            }
            itemArray.add(n);
        }
        for (int i=0;i<car.length;i++){
            String n=car[i];
            if (i>0){
                n="    "+n;
            }
            itemArray.add(n);
        }

        for (int i=0;i<edu.length;i++){
            String n=edu[i];
            if (i>0){
                n="    "+n;
            }
            itemArray.add(n);
        }
        for (int i=0;i<extra.length;i++){
            String n=extra[i];
            if (i>0){
                n="    "+n;
            }
            itemArray.add(n);
        }
        xfocrStart=itemArray.size()+1;
        for (int i=0;i<xfocr.length;i++){
            String n=xfocr[i];
            if (i>0){
                n="    "+n;
            }
            itemArray.add(n);
        }
        return itemArray;
    }
    public void addArrays(ArrayList<String> temps ,String... params){
        for (int i=0;i<params.length;i++){
            temps.add(params[i]);
        }
    }
    public OCRTypes getCommonTypes(String menu){
        String mn=menu.trim();
        String[] commons=new String[]{"简单","简单(含位置)","高精度(含生僻字)","高精度(含位置)"};
        ArrayList<String> temps=new ArrayList<>();
        addArrays(temps,commons);
        OCRTypes ocrType=null;
        if (temps.contains(mn)){
            int index=temps.indexOf(mn);
            String branch="";
            switch (index){
                case 0:
                    branch=OCRTypes.OCRCommonBranch.type_general_basic;
                    break;
                case 1:
                    branch=OCRTypes.OCRCommonBranch.type_general;
                    break;
                case 2:
                    branch=OCRTypes.OCRCommonBranch.type_accurate_basic;
                    break;
                case 3:
                    branch=OCRTypes.OCRCommonBranch.type_accurate;
                    break;
            }
            ocrType=new OCRTypes(OCRTypes.ocr_type_common,branch);
        }
        return ocrType;
    }
    public OCRTypes getcardTypes(String menu){
        String mn=menu.trim();
        String[] commons=new String[]{"身份证(正面)","身份证(反面)","银行卡","营业执照","名片","护照","港澳通行证","台湾通行证识别","户口本识别","出生医学证明识别","多卡证类别检测"};
        ArrayList<String> temps=new ArrayList<>();
        addArrays(temps,commons);
        OCRTypes ocrType=null;
        if (temps.contains(mn)){
            String branch="";
            int index=temps.indexOf(mn);
            int fontorback=0;
            switch (index){
                case 0:
                    branch=OCRTypes.OCRCardBranch.type_idcard;
                    break;
                case 1:
                    branch=OCRTypes.OCRCardBranch.type_idcard;
                    fontorback=1;
                    break;
                case 2:
                    branch=OCRTypes.OCRCardBranch.type_bankcard;
                    break;
                case 3:
                    branch=OCRTypes.OCRCardBranch.type_business_license;
                    break;
                case 4:
                    branch=OCRTypes.OCRCardBranch.type_business_card;
                    break;
                case 5:
                    branch=OCRTypes.OCRCardBranch.type_passport;
                    break;
                case 6:
                    branch=OCRTypes.OCRCardBranch.type_HK_Macau_exitentrypermit;
                    break;
                case 7:
                    branch=OCRTypes.OCRCardBranch.type_taiwan_exitentrypermit;
                    break;
                case 8:
                    branch=OCRTypes.OCRCardBranch.type_household_register;
                    break;
                case 9:
                    branch=OCRTypes.OCRCardBranch.type_birth_certificate;
                    break;
                case 10:
                    branch=OCRTypes.OCRCardBranch.type_multi_card_classify;
                    break;
            }
            ocrType=new OCRTypes(OCRTypes.ocr_type_card,branch,fontorback);
        }
        return ocrType;
    }
    public OCRTypes getbillTypes(String menu){
        String mn=menu.trim();
        String[] commons=new String[]{"增值税发票","定额发票","通用机打发票","火车票","出租车票","行程单","汽车票","通行费发票","通用票据","保单","彩票"};
        ArrayList<String> temps=new ArrayList<>();
        addArrays(temps,commons);
        OCRTypes ocrType=null;
        if (temps.contains(mn)){
            String branch="";
            int index=temps.indexOf(mn);
            switch (index){
                case 0:
                    branch=OCRTypes.OCRBillBranch.type_vat_invoice;
                    break;
                case 1:
                    branch=OCRTypes.OCRBillBranch.type_quota_invoice;
                    break;
                case 2:
                    branch=OCRTypes.OCRBillBranch.type_invoice;
                    break;
                case 3:
                    branch=OCRTypes.OCRBillBranch.type_train_ticket;
                    break;
                case 4:
                    branch=OCRTypes.OCRBillBranch.type_taxi_receipt;
                    break;
                case 5:
                    branch=OCRTypes.OCRBillBranch.type_air_ticket;
                    break;
                case 6:
                    branch=OCRTypes.OCRBillBranch.type_bus_ticket;
                    break;
                case 7:
                    branch=OCRTypes.OCRBillBranch.type_toll_invoice;
                    break;
                case 8:
                    branch=OCRTypes.OCRBillBranch.type_receipt;
                    break;
                case 9:
                    branch=OCRTypes.OCRBillBranch.type_insurance_documents;
                    break;
                case 10:
                    branch=OCRTypes.OCRBillBranch.type_lottery;
                    break;
            }
            ocrType=new OCRTypes(OCRTypes.ocr_type_bill,branch);
        }
        return ocrType;
    }
    public OCRTypes getcarTypes(String menu){
        String mn=menu.trim();
        String[] commons=new String[]{"行驶证","驾驶证","车牌","VIN码","机动车销售发票","车辆合格证"};
        ArrayList<String> temps=new ArrayList<>();
        addArrays(temps,commons);
        OCRTypes ocrType=null;
        if (temps.contains(mn)){
            String branch="";
            int index=temps.indexOf(mn);
            switch (index) {
                case 0:
                    branch = OCRTypes.OCRCarBranch.type_vehicle_license;
                    break;
                case 1:
                    branch = OCRTypes.OCRCarBranch.type_driving_license;
                    break;
                case 2:
                    branch = OCRTypes.OCRCarBranch.type_license_plate;
                    break;
                case 3:
                    branch = OCRTypes.OCRCarBranch.type_vin_code;
                    break;
                case 4:
                    branch = OCRTypes.OCRCarBranch.type_vehicle_invoice;
                    break;
                case 5:
                    branch = OCRTypes.OCRCarBranch.type_vehicle_certificate;
                    break;
            }
            ocrType=new OCRTypes(OCRTypes.ocr_type_car,branch);
        }
        return ocrType;
    }
    public OCRTypes geteduTypes(String menu){
        String mn=menu.trim();
        String[] commons=new String[]{"手写文字","公式"};
        ArrayList<String> temps=new ArrayList<>();
        addArrays(temps,commons);
        OCRTypes ocrType=null;
        if (temps.contains(mn)){
            String branch="";
            int index=temps.indexOf(mn);
            switch (index) {
                case 0:
                    branch = OCRTypes.OCREducateBranch.type_handwriting;
                    break;
                case 1:
                    branch = OCRTypes.OCREducateBranch.type_formula;
                    break;
            }
            ocrType=new OCRTypes(OCRTypes.ocr_type_educate,branch);
        }
        return ocrType;
    }
    public OCRTypes getextraTypes(String menu){
        String mn=menu.trim();
        String[] commons=new String[]{"网络图片文字","网络图片文字(含位置版)","文档版面","仪器仪表盘读数","表格文字(异步接口)","表格文字(同步接口)","数字","二维码","印章","门脸文字"};
        ArrayList<String> temps=new ArrayList<>();
        addArrays(temps,commons);
        OCRTypes ocrType=null;
        if (temps.contains(mn)){
            String branch="";
            int index=temps.indexOf(mn);
            switch (index) {
                case 0:
                    branch = OCRTypes.OCRExtraBranch.type_webimage;
                    break;
                case 1:
                    branch = OCRTypes.OCRExtraBranch.type_webimage_loc;
                    break;
                case 2:
                    branch = OCRTypes.OCRExtraBranch.type_doc_analysis;
                    break;
                case 3:
                    branch = OCRTypes.OCRExtraBranch.type_meter;
                    break;
                case 4:
                    branch = OCRTypes.OCRExtraBranch.type_request;
                    break;
                case 5:
                    branch = OCRTypes.OCRExtraBranch.type_form;
                    break;
                case 6:
                    branch = OCRTypes.OCRExtraBranch.type_numbers;
                    break;
                case 7:
                    branch = OCRTypes.OCRExtraBranch.type_qrcode;
                    break;
                case 8:
                    branch = OCRTypes.OCRExtraBranch.type_seal;
                    break;
                case 9:
                    branch = OCRTypes.OCRExtraBranch.type_facade;
                    break;
            }
            ocrType=new OCRTypes(OCRTypes.ocr_type_extra,branch);
        }
        return ocrType;
    }
    public OCRTypes getXfTypes(String menu){
        String mn=menu.trim();
        String[] commons=new String[]{"手写文字识别","印刷文字识别","印刷文字识别（多语种）","名片识别","身份证识别","银行卡识别","营业执照识别","增值税发票识别","拍照速算识别","公式识别","指尖文字识别"};
        ArrayList<String> temps=new ArrayList<>();
        addArrays(temps,commons);
        OCRTypes ocrType=null;
        if (temps.contains(mn)) {
            String branch = "";
            int index = temps.indexOf(mn);
            switch (index) {
                case 0:
                    branch = OCRTypes.OCRXFBranch.type_handwriting;
                    break;
                case 1:
                    branch = OCRTypes.OCRXFBranch.type_general;
                    break;
                case 2:
                    branch = OCRTypes.OCRXFBranch.type_recognize_document;
                    break;

                case 3:
                    branch = OCRTypes.OCRXFBranch.type_business_card;
                    break;
                case 4:
                    branch = OCRTypes.OCRXFBranch.type_idcard;
                    break;
                case 5:
                    branch = OCRTypes.OCRXFBranch.type_bankcard;
                    break;

                case 6:
                    branch = OCRTypes.OCRXFBranch.type_business_license;
                    break;
                case 7:
                    branch = OCRTypes.OCRXFBranch.type_invoice;
                    break;
                case 8:
                    branch = OCRTypes.OCRXFBranch.type_itr;
                    break;
                case 9:
                    branch = OCRTypes.OCRXFBranch.type_itr_et;
                    break;
                case 10:
                    branch = OCRTypes.OCRXFBranch.type_ocr;
                    break;
            }
            ocrType = new OCRTypes(OCRTypes.ocr_type_xunfei, branch);
        }
        return ocrType;
    }
    public OCRTypes getTypes(String menu){
        OCRTypes ocrType=null;
        ocrType=getCommonTypes(menu);
        if (ocrType==null){
            ocrType=getcardTypes(menu);
            if (ocrType==null){
                ocrType=getcarTypes(menu);
                if (ocrType==null){
                    ocrType=getbillTypes(menu);
                    if (ocrType==null){
                        ocrType=geteduTypes(menu);
                        if (ocrType==null){
                            ocrType=getextraTypes(menu);
                        }
                    }
                }
            }
        }
        return ocrType;
    }
}